var $ = getApp(), methods = require("../methods.js");
Page(Object.assign({}, methods, {
	data: {
		datalist: [],
		content_pic:[],
		pagenum: 1,
		cateArr:[],
		cindex:-1,
		video:'',
	},
	_onLoad:function(){
		var that = this
		$.get('luntan/fatie',{},function(res){
			if(res.status == 0){
				$.alert(res.msg);
				return ;
			}
			that.setData(res)
			var clist = res.clist
			if(clist.length > 0){
				var cateArr = [];
				for(var i in clist){
					if(that.options && that.options.cid == clist[i].id){
						that.setData({cindex:i})
					}
					cateArr.push(clist[i].name)
				}
			}else{
				cateArr = false
			}
			that.setData({cateArr:cateArr})
		})
	},
	cateChange:function(e){
		this.setData({cindex:e.detail.value})
	},
	formsubmit:function(e){
		var that = this
		console.log(e)
		var clist = that.data.clist;
		if(clist.length>0){
			if(that.data.cindex == -1){
				$.error('请选择分类');return false;
			}
			var cid = clist[that.data.cindex].id
		}else{
			var cid = 0;
		}
		var formdata = e.detail.value
		var content = formdata.content
		var pics = formdata.pics
		var video = formdata.video
		if(content=='' && pics==''){
			$.error('请输入内容');return false;
		}
		$.post('luntan/fatie',{cid:cid,pics:pics,content:content,video:video},function(res){
			$.loading(false);
			if(res.status==1){
				$.success(res.msg);
				setTimeout(function(){
					var pages = getCurrentPages();
					if (pages.length > 1) {
						var prePage = pages[pages.length - 2];
						prePage._onLoad();
					}
					$.goback();
				},1000)
			}else{
				$.error(res.msg)	
			}
		})
	},
	uploadvideo:function(){
		var that = this
		console.log(11)
		wx.chooseVideo({
			sourceType: ['album', 'camera'],
			maxDuration: 60,
			success: function (res){
				var tempFilePath = res.tempFilePath;
				$.loading('上传中');
				wx.uploadFile({
					url: $.config.baseurl + 'Imageupload/uploadImg/aid/'+$.aid+'/platform/'+$.platform+'/session_id/' + $.config.session_id,
					filePath: tempFilePath,
					name: 'file',
					success: function (res) {
						$.loading(false);
						var data = JSON.parse(res.data);
						if(data.status == 1){
							that.setData({video:data.url});
						} else {
							$.alert(data.msg);
						}
					},
					fail: function (res) {
						$.loading(false);
						$.alert(res.errMsg);
					}
				});
			},
			fail: function (res) {
				console.log(res)
				//alert(res.errMsg);
			}
		})
	}
}))